//
//  LPSearchViewController.m
//  GongXinDangJian
//
//  Created by 刘应平 on 2017/12/12.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "LPSearchViewController.h"
#import "LPDCDetailFirstTableViewCell.h"
#import "LPDCDetailFourTableViewCell.h"
#import "LPDCDetailThreeTableViewCell.h"
#import "LPDCDetailSecondTableViewCell.h"
#import "LPDCDetailModel.h"
#import "LPWKHasCommentAndHeadLineViewController.h"
@interface LPSearchViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  搜索框  */
@property (strong, nonatomic) UITextField  *searchTextField;
@end

@implementation LPSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
   [self.navigView addSubview:self.searchTextField];
    [self layoutUI];
}

#pragma mark - 数据
- (void)getDataSouce{
    __weak typeof(self) weakSelf = self;
   
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"KEYWORD"] = self.searchTextField.text;
    paramer[@"PAGESTART"] = @"1";
    paramer[@"PAGECOUNT"] = @"10";
    
    [NetWorkingManager postWithUrlString:@"solr.FindDjNewsBySolr" Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            
            NSArray *array = [object objectForKey:@"NEWS"];
            self.dataSouce = [[NSArray yy_modelArrayWithClass:[LPDCDetailModel class] json:array] mutableCopy];
            [weakSelf.tableView reloadData];
            
        }else{
            [YJProgressHUD showSuccess:@"加载数据失败" inview:weakSelf.view];
            
        }
        
        
    } Fail:^(id object) {
        
       
        [YJProgressHUD showSuccess:@"网络错误" inview:weakSelf.view];
    }];
}

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)_page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"KEYWORD"] = self.searchTextField.text;
    paramer[@"PAGESTART"] = page;
    paramer[@"PAGECOUNT"] = @"10";
    
    [NetWorkingManager postWithUrlString:@"solr.FindDjNewsBySolr" Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            
            NSArray *array = [object objectForKey:@"NEWS"];
            self.dataSouce = [[NSArray yy_modelArrayWithClass:[LPDCDetailModel class] json:array] mutableCopy];
            [weakSelf.tableView reloadData];
            
        }else{
            [YJProgressHUD showSuccess:@"加载数据失败" inview:weakSelf.view];
            
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:weakSelf.view];
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)_page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"KEYWORD"] = self.searchTextField.text;
    paramer[@"PAGESTART"] = page;
    paramer[@"PAGECOUNT"] = @"10";
    
    [NetWorkingManager postWithUrlString:@"solr.FindDjNewsBySolr" Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"NEWS"];
            if (array.count == 0) {
                _page --;
            }else{
                [self.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPDCDetailModel class] json:array]];
                [weakSelf.tableView reloadData];
                
            }
            
            
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
}

#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length == 0) {
        return;
    }
    _page = 1;
    [self getDataSouce];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toBeString.length >= 50 && range.length!= 1 ){
        textField.text = [toBeString substringToIndex:50];
        return NO;
        
    }
    return YES;
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LPDCDetailModel *model = self.dataSouce[indexPath.row];
    return model.CELLHEIGHT;
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    LPDCDetailModel *model = self.dataSouce[indexPath.row];
    if ([model.DISPLAY isEqualToString:@"3"]) {
        LPDCDetailFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[LPDCDetailFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
        
    }else if ([model.DISPLAY isEqualToString:@"1"]){
        LPDCDetailSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[LPDCDetailSecondTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }else if ([model.DISPLAY isEqualToString:@"2"]){
        LPDCDetailThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[LPDCDetailThreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }else {
        LPDCDetailFourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[LPDCDetailFourTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }
    
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LPDCDetailModel *model = self.dataSouce[indexPath.row];
    LPWKHasCommentAndHeadLineViewController *vc = [LPWKHasCommentAndHeadLineViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, navHeight, self.view.bounds.size.width, [UIScreen zj_screenHeight] -  navHeight ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}



- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

- (UITextField *)searchTextField{
    if (!_searchTextField) {
        _searchTextField = ({
            UITextField *textField =  [[UITextField alloc] initWithFrame:CGRectMake(55 * [UIScreen zj_screenWScale], 8 + LP_StatusBarHeight, [UIScreen zj_screenWith] - 110 * [UIScreen zj_screenWScale], 28)];
            textField.placeholder = @"输入关键字快速查找内容";
            textField.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
            textField.textColor = [UIColor textBlackColor];
            textField.textAlignment = NSTextAlignmentLeft;
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 17)];
            image.image = [UIImage imageNamed:@"learn_soso"];
            image.contentMode = UIViewContentModeCenter;
            textField.leftView = image;
            textField.leftViewMode = UITextFieldViewModeAlways;
            [textField setTextFieldRightforWidth:10 * [UIScreen zj_screenWScale]];
            textField.backgroundColor = [UIColor whiteColor];
            textField.layer.cornerRadius = 12;
            textField.layer.masksToBounds = YES;
            textField.delegate = self;
            textField.returnKeyType = UIReturnKeySearch;
            [textField becomeFirstResponder];
            textField;
        });
        
    }
    return _searchTextField;
    
}

@end
